﻿/*
 *       This file is part of VFC.
 *
 *        VFC is free software: you can redistribute it and/or modify
 *        it under the terms of the GNU General Public License as published by
 *        the Free Software Foundation, either version 3 of the License, or
 *        (at your option) any later version.
 *
 *        VFC is distributed in the hope that it will be useful,
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *        GNU General Public License for more details.
 *
 *        You should have received a copy of the GNU General Public License
 *        along with VFC.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using VFC.Emu;

namespace VFC
{
    public partial class frmMain : Form
    {
        _Console con = new _Console(VFC.Config.App.ConsoleTitle);

        public frmMain()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(frmMain_Paint);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
        }

        private void frmMain_Paint(object sender, EventArgs e)
        {

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //Loading Config...
            this.Text = VFC.Config.App.WindowTitle;

            //End Config.......

            con.Show();
            //Console.WriteLine(Convert.ToString(Convert.ToInt32("00000001",2),16));

            //Initialize EmuCore...
            EmuCore.Init();
            mode_software.Checked = true;
            CmdPlay.Enabled = true;
            CmpPause.Enabled = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.Show();
        }

        private void OpenRoom_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Nes Files (*.nes)|*.nes|All Files (*.*)|*.*";
            ofd.InitialDirectory = Application.StartupPath;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //Load game rom...
                EmuCore.LoadRom(ofd.FileName);
            }
        }

        private void romViewerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmRomViewer frv = new frmRomViewer();
            frv.Show();
        }

        private void memoryViewerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMemViewer frmMemV = new frmMemViewer();
            frmMemV.Show();
        }

        private void CmdPlay_Click(object sender, EventArgs e)
        {
            CmdPlay.Enabled = false;
            CmpPause.Enabled = true;
            EmuCore.Run();
        }

        private void mode_software_Click(object sender, EventArgs e)
        {
            mode_software.Checked = true;
            mode_hardware.Checked = false;
            Config.Video.Mode = 0; //software mode...
            Console.WriteLine("Video mode set to Software");
        }

        private void mode_hardware_Click(object sender, EventArgs e)
        {
            mode_software.Checked = false;
            mode_hardware.Checked = true;
            Config.Video.Mode = 1; //hardware mode...
            Console.WriteLine("Video mode set to Hardware");
        }

        private void debuggerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDebug debug = new frmDebug();
            debug.Show();
        }

        private void CmpPause_Click(object sender, EventArgs e)
        {
            CmdPlay.Enabled = true;
            CmpPause.Enabled = false;
            EmuCore.Stop();
        }

        private void Anim_Tick(object sender, EventArgs e)
        {
            //Run Emulation Core...
            EmuCore.Execute();
        }

        private void frmMain_FormClosing(object sender, EventArgs e)
        {
            try
            {
                Console.WriteLine("Stoping CoreThread");
                EmuCore.Stop();
            }
            catch
            {
                //do nothing....
            }
        }
    }
}
